package com.itheima;

import com.mysql.cj.jdbc.Driver;

import java.sql.*;

public class JDBCDemo {

    /**
     * 演示JDBC的操作步骤
     *
     * @param args
     */
    public static void main(String[] args) throws SQLException {
        //1. 导入mysql数据库的驱动包

        //2. 注册mysql驱动
        DriverManager.registerDriver(new Driver());

        //3. 获取数据库连接对象
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo1",
                "root",
                "tjy20040512@");

        //4. 获取执行sql语句的对象
        PreparedStatement pt = conn.prepareStatement("select  * from user");

        //5. 执行sql语句，并接收返回结果
        ResultSet rs = pt.executeQuery();

        //6. 处理结果集
        while (rs.next()) {
            //获取数据
            int id = rs.getInt("id");
            String name = rs.getString("name");
            String password = rs.getString("password");
            int age = rs.getInt("age");
            String address = rs.getString("address");

            System.out.println(id + " " + name + " " + password + " " + age + " " + address);
        }

        //7. 释放资源
        rs.close();
        pt.close();
        conn.close();
    }
}